﻿<%@ Page Language="C#" AutoEventWireup="false" MasterPageFile="~/SIPO.master" CodeFile="sRolelist.aspx.cs" Inherits="SIPO.Web.Lists.Administration.UserManagement.sRolelist" %>

<asp:Content ID="ctnBody" ContentPlaceHolderID="body" runat="server">

    <link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/styles/ui.jqgrid.css") %>" />
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>
    <script src="<%= ResolveUrl("~/scripts/helper.js") %>" type="text/javascript"></script>
    <div>
 
        <button id="btnAdd" type="button" onclick="OpenForm('add');">Tambah</button>
        <button id="btnEdit" type="button" onclick="OpenForm('edit');">Ubah</button>
        <button id="btnDelete" type="button" onclick="OpenForm('delete');">Hapus</button>

    </div>
    <br />
    <br />
    <table id="table" class="scroll" cellpadding="0" cellspacing="0">
    </table>
    <div id="pager" class="scroll">
    </div>
    <br />
    <div id="dialog" title="Default dialog">
    </div>
    <asp:HiddenField ID="HfStatus" runat="server" />
    <script type="text/javascript">

        $("#btnAdd").button({ icons: { primary: "ui-icon-plus"} });
        $("#btnEdit").button({ icons: { primary: "ui-icon-pencil"} });
        $("#btnDelete").button({ icons: { primary: "ui-icon-trash"} });

    </script>

    <script type="text/javascript">

        var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Administration/UserManagement/sRolehandler.ashx") %>';
        var status = $('#<%= HfStatus.ClientID %>').val();

        if (status == -1) {
            getData("getdatainternal");
        } else {
            getData("getdataexternal");
        }

        function getData(methodName) {
            $('#table').jqGrid({
                url: handlerUrl + '?method=' + methodName,
                datatype: 'json',
                colNames: ['ID', 'Nama Role', 'User'],
                colModel: [
           		{ name: 'RoleID', key: true, hidden: true, index: 'ROLEID', width: 60 },
           		{ name: 'RoleName', index: 'ROLENAME', width: 200, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'UserID', index: 'USERID', search: false, align: 'center', sortable: false, width: 20 }
           	],
                rowNum: 10,
                id: 'RoleID',
                multiselect: true,
                rowList: [5, 10, 15, 20],
                pager: '#pager',
                sortname: 'ROLENAME',
                sortorder: "asc",
                viewrecords: true,
                jsonReader: { repeatitems: false },
                width: 600,
                height: '100%',
                caption: 'Role',
                ajaxGridOptions: { cache: false },
                ondblClickRow: function () {
                    OpenForm('edit');
                },
                gridComplete: function () {
                    var ids = $('#table').jqGrid('getDataIDs');
                    for (var i = 0; i < ids.length; i++) {
                        var _id = $('#table').jqGrid('getCell', ids[i], 'RoleID');
                        var _name = $('#table').jqGrid('getCell', ids[i], 'RoleName');

                        idx = "<a href='javascript:void(0);' class='dialog_link ui-corner-all' title='User' onclick=\"onclickButtonUser('" + _id + "', '" + _name + " ');\"><span class='ui-icon ui-icon-newwin'></span>...</a>";

                        $('#table').jqGrid('setRowData', ids[i], { UserID: idx });
                    }
                }
            });
        }

        $("#table").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false, search:true, searchtext: 'Search', refreshtext: 'Refresh' },
        {}, {}, {}, { closeOnEscape: true, closeAfterSearch: true });

        function onclickButtonUser(id, name) {
            $('#table').jqGrid('resetSelection');

            var urlList = '<%= ResolveUrl("~/lists/administration/usermanagement/sRoleUserlist.aspx") %>';
            $('#dialog').load(urlList + '?' + 'method=getdata&roleId=' + id + '&menuId=' + '<%= base.MenuID %>').dialog({ autoOpen: false, modal: true, width: 530, height: 420, title: 'User Of ' + name });
            $('#dialog').dialog('open');
        }

        function OpenForm(ctl) {

            var formUrl = '<%= ResolveUrl("~/forms/administration/usermanagement/sRoleform.aspx") %>';

            var grid = $('#table');
            var selIds = grid.jqGrid('getGridParam', 'selarrrow'),
                selText = [];
            $.each(selIds, function () {
                selText.push(grid.jqGrid('getCell', this, 'RoleID'));
            });

            if (ctl == 'edit') {
                if (selIds.length == 0) {
                    alert('Pilih data yang mau di ubah !');
                }
                else if (selIds.length > 1) {
                    alert('Pilih satu record untuk ubah data.');
                }
                else {
                    $('#dialog').load(formUrl + '?id=' + selText + '&Status=' + status +'&menuId=' + '<%= base.MenuID %>').dialog({ autoOpen: false, modal: true, width: 400, height:130, title: 'Ubah Role' });
                    $("#dialog").dialog('open');
                }
            }
            else if (ctl == 'add') {
                $('#dialog').load(formUrl + '?menuId=' + '<%= base.MenuID %>' + '&Status=' + status).dialog({ autoOpen: false, modal: true, width: 400, height: 130, title: 'Tambah Role' });
                $("#dialog").dialog('open');
            }
            else {

                if (selIds.length == 0)
                    alert('Pilih data yang mau di hapus !');
                else {
                    var result = window.confirm('Benar data akan di hapus ?');
                    if (result) {

                        var objRole = new Object();
                        objRole.RoleID = encodeURIComponent(selText);

                        jsonData = JSON.stringify(objRole);

                        CallAjax('<%= ResolveUrl("~/ajaxhandler/administration/usermanagement/sRolehandler.ashx") %>', "?method=delete", jsonData);

                        setTimeout("$('#table').trigger('reloadGrid');", 3000);
                    }
                }
            }

        }
    
    </script>
</asp:Content>